package cn.lanqiao.springboot.service;

import cn.lanqiao.springboot.entity.User;
import cn.lanqiao.springboot.mapper.UserMapper;
import cn.lanqiao.springboot.mapper.UserRoleMapper;
import cn.lanqiao.springboot.vo.QueryUserVO;
import cn.lanqiao.springboot.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    public UserVO checkUser(User user) {
        return userMapper.checkUser(user);
    }

    @Transactional
    public void insert(UserVO userVO) {
        userMapper.insert(userVO);
        userRoleMapper.insert(userVO);
    }

    @Transactional
    public void delete(int userId) {
        userMapper.delete(userId);
        userRoleMapper.delete(userId);
    }

    @Transactional
    public void deleteAll(String[] ids) {
        for (String userId : ids) {
            userMapper.delete(Integer.parseInt(userId));
            userRoleMapper.delete(Integer.parseInt(userId));
        }
    }

    @Transactional
    public void update(UserVO userVO) {
        userMapper.update(userVO);
        userRoleMapper.update(userVO);
    }

    public List<UserVO> getUserList(QueryUserVO queryUserVO) {
        return userMapper.getUserList(queryUserVO);
    }

    public UserVO getUserByPhone(String phone) {
        return userMapper.getUserByPhone(phone);
    }
}
